﻿@using EmployeeLeaveSystem.Shared;
@using EmployeeLeaveSystem.Shared.Models;
@inject IMessageService _MessageService;
@inject HttpClient _HttpClient;

<Form Model="addWOM"
      Layout="@FormLayout.Vertical"
      OnFinish="UpLoad">
    <FormItem Label="姓名">
        @GobalManager.myInfo?.Name
    </FormItem>
    <FormItem Label="申请类型">
        <EnumSelect @bind-Value="context.Type" TEnum="ESModel.AddWOMType" />
    </FormItem>
    <FormItem Label="审核人">
        <Select @bind-DataSource="staffInfos"
                TItem="StaffInfo"
                TItemValue="int?"
                @bind-Value="context.AcceptStaffId"
                LabelName="@nameof(Department.Name)"
                ValueName="@nameof(Department.Id)"
                Placeholder="选择一个审核人"
                Loading="@_SelectLoading"
                OnFocus="@SelectFocus">
        </Select>
    </FormItem>
    <FormItem Label="申请时间">
        <RangePicker ShowTime='@("HH:mm")' TValue="DateTime?[]" @bind-Value="_selectDate" OnChange="()=>{InvokeAsync(StateHasChanged);}" />
        @if (_selectDate != null) {
            <div>总计：@((_selectDate[1] - _selectDate[0]).Value.Hours)小时</div>
        }
    </FormItem>
    <FormItem Label="描述">
        <TextArea @bind-Value="context.Reason" />
    </FormItem>

    <FormItem>
        <Button Block Type="@ButtonType.Primary" HtmlType="submit">
            提交
        </Button>
        </FormItem>
        </Form>

        @using ESModel = EmployeeLeaveSystem.Shared.Models;
        @code {
    ESModel.AddWOM addWOM = new();
    DateTime?[] _selectDate;
    List<StaffInfo> staffInfos = new();

    protected override async Task OnInitializedAsync() {
        _selectDate = new DateTime?[2];
        _selectDate[0] = addWOM.StartTime = DateTime.Now;
        _selectDate[1] = addWOM.EndTime = DateTime.Now.AddHours(2);
    }

    async void UpLoad() {
        addWOM.StaffId = GobalManager.myInfo.Id;
        await _HttpClient.PostAsJsonAsync<ESModel.AddWOM>("api/OverTime/AddOverTime", addWOM);
        _MessageService.Success("上传成功");
    }

    bool _SelectLoading = false;
    async void SelectFocus() {
        _SelectLoading = true;
        _MessageService.Info("加载审核人信息中");
        await App._appHandle.GetAllStaffInfos();
        staffInfos.Clear();
        for (int i = 0; i < GobalManager.staffInfos.Count; i++) {
            var infos = GobalManager.staffInfos[i];
            if (GobalManager.staffInfos[i].Type == StaffType.Manager) {
                staffInfos.Add(infos);
            }
        }
        _MessageService.Success("加载完成");
        _SelectLoading = false;
        InvokeAsync(StateHasChanged);
    }
}
